//
//  NSArray_NSDictionary+SPLog.m
//  SPKit
//
//  Created by 李双鹏 on 15/10/7.
//  Copyright (c) 2015年 SP. All rights reserved.
//

#import "NSArray_NSDictionary+SPLog.h"
#import "NSString+ContentVerify.h"

@implementation NSArray (SPLog_Implementation)
// level:start from 0.
-(NSString *)logFromLevel:(NSUInteger)level toLevel:(NSUInteger)limit start:(NSUInteger)start{
    if (level > limit) {
        
        return @"[...],\n";
        
    }
    NSMutableString *string = [NSMutableString stringWithFormat:@"[\n"];
    NSMutableString *prefix = [NSMutableString stringWithString:@"  "];
    for (int i = 0; i < level; i++) {
        [prefix appendString:@"  "];
    }
    for (int i = 0; i < start; i++) {
        [prefix appendString:@" "];
    }
    [self enumerateObjectsUsingBlock:^(id value, NSUInteger idx, BOOL *stop) {
        [string appendString:prefix];
        if ([value isKindOfClass:[NSNumber class]]) {
            [string appendFormat:@"%@,\n",value];
        }else if ([value isKindOfClass:[NSString class]]) {
            [string appendFormat:@"\"%@\",\n",value];
        }else if ([value isKindOfClass:[NSDictionary class]]){
            [string appendString:[value logFromLevel:level+1 toLevel:limit start:start]];
        }else if([value isKindOfClass:[NSArray class]]){
            [string appendString:[self logFromLevel:level+1 toLevel:limit start:start]];
        }else if ([value isKindOfClass:[NSNull class]]){
            [string appendString:@"null,\n"];
        }else{
//            *stop = YES;
            NSString *class = NSStringFromClass([value class]);
            NSString *temp = [value description];
            if (![temp containsString:class]) {
                temp = [NSString stringWithFormat:@"<%@:%@>,\n",class,temp];
                [string appendString:temp];
            }else{
                [string appendString:temp];
                [string appendString:@",\n"];
            }
        }
    }];
    if (self.count) {
        [string deleteCharactersInRange:NSMakeRange(string.length - 2, 1)];
    }
    [string appendString:prefix];
    [string deleteCharactersInRange:NSMakeRange(string.length - 2, 2)];

    if (level) {
        [string appendString:@"],\n"];
    }else{
        [string appendString:@"]\n"];
    }
    return string;
}
@end

@implementation NSDictionary (SPLog_Implementation)

// level:start from 0.
-(NSString *)logFromLevel:(NSUInteger)level toLevel:(NSUInteger)limit start:(NSUInteger)start{
    if (level > limit) {
        return @"{...},\n";
    }
    NSMutableString *string = [NSMutableString stringWithFormat:@"{\n"];
    NSMutableString *prefix = [NSMutableString stringWithString:@"  "];
    for (int i = 0; i < level; i++) {
        [prefix appendString:@"  "];
    }
    for (int i = 0; i < start; i++) {
        [prefix appendString:@" "];
    }
    @autoreleasepool {
        [self enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL *stop) {
            NSString *temp;
            [string appendString:prefix];
            temp = [NSString stringWithFormat:@"\"%@\":",key];
            [string appendString:temp];
            
            if ([value isKindOfClass:[NSNumber class]]) {
                [string appendFormat:@"%@,\n",value];
            }else if ([value isKindOfClass:[NSString class]]) {
                [string appendFormat:@"\"%@\",\n",value];
            }else if ([value isKindOfClass:[NSDictionary class]]){
                [string appendString:[value logFromLevel:level+1 toLevel:limit start:temp.length + start]];
            }else if([value isKindOfClass:[NSArray class]]){
                [string appendString:[value logFromLevel:level + 1 toLevel:limit start:temp.length + start]];
            }else if ([value isKindOfClass:[NSNull class]]){
                [string appendFormat:@"null,\n"];
            }else{
//                *stop = YES;
                NSString *class = NSStringFromClass([value class]);
                temp = [value description];
                if (![temp containsString:class]) {
                    temp = [NSString stringWithFormat:@"<%@:%@>,\n",class,temp];
                    [string appendString:temp];
                }else{
                    [string appendString:temp];
                    [string appendString:@",\n"];
                }
            }
        }];
    }
    if (self.count) {
        [string deleteCharactersInRange:NSMakeRange(string.length - 2, 1)];
    }
    [string appendString:prefix];
    [string deleteCharactersInRange:NSMakeRange(string.length - 2, 2)];
    if (level) {
        [string appendString:@"},\n"];
    }else{
        [string appendString:@"}\n"];
    }
    
    return string;
}

@end

//NSUInteger spAbs(NSInteger x){
//    if (x < 0) {
//        return -x;
//    }else{
//        return x;
//    }
//}